package com.itzz.spring_zhoneHeng.controller;

import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.OrderDao;
import com.itzz.spring_zhoneHeng.dao.UserDao;
import com.itzz.spring_zhoneHeng.pojo.BookShelf;
import com.itzz.spring_zhoneHeng.pojo.Order;
import com.itzz.spring_zhoneHeng.service.BookShelfService;
import com.itzz.spring_zhoneHeng.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderDao orderDao;

    @Autowired
    UserDao userDao;

    //添加订单
    @RequestMapping("/addOrder")
    public RestResponse addOrder(Order order,Integer bookId,Integer userId){
        RestResponse response = orderService.addOrder(order,bookId,userId);
        return response;
    }


    //查询所有的订单
    @RequestMapping("/queryAllOrder")
    public RestResponse queryAllOrder(Order order, Integer pageNum, Integer pageSize){
        RestResponse response = orderService.queryAllOrder(order,pageNum,pageSize);
        return response;
    }


    //根据订单id删除订单
    @RequestMapping("/delOrder/{id}")
    public RestResponse delOrder(@PathVariable("id") Integer id){
        RestResponse response = orderService.delOrder(id);
        return response;
    }
    //根据订单id付钱
    @RequestMapping("/pay/{id}")
    public RestResponse pay
    (@RequestBody HashMap<String,Object> map,@PathVariable("id") Integer id){

        RestResponse response = orderService.updateOrder(map,id);
        return response;
    }

    //传输过来的时bookid
    //根据bookid查询 o_b表 是否有 order oid  如果oid为空 则返回1  否则返回2
    @RequestMapping("/queryOrderState")
    public RestResponse queryOrderState(Integer bookId,Integer userId){
        //根据bookId 和userId 查询order表中的state
        Integer orderState = orderDao.queryOrderState(bookId, userId);

        //如果查询到orderState 不存在  就是确定不存在本订单
        if(orderState==null){
          return   RestResponse.fail(0,"订单不存在");
        }
        //存在本订单就返回
        return   RestResponse.ok(orderState);
    }


    //根据用户名查询余额
    @RequestMapping("/queryMoney")
    public RestResponse queryMoney(String username){
        Double money = userDao.queryMoney(username);
        if(money!=null&&money>=20.0){
            return  RestResponse.ok(money);
        }
        return RestResponse.fail(0,"余额不足");
    }

//根据订单id查询bookid
    @RequestMapping("/queryBookId/{id}")
    public RestResponse queryBookId(@PathVariable("id") Integer id){
        Integer bookid = orderDao.queryBookId(id);
        if(bookid!=null){
            return  RestResponse.ok(bookid);
        }
        return RestResponse.fail(0,"没有此书");
    }





}
